package TeamService;

import Teamdomain.*;

import static TeamService.Data.*;
import Teamdomain.Designer.*;

/**
 * @program: Demo2
 * @description:
 * @author: yan guang
 * @create: 2020-04-22 10:17
 */
public class NameListService {
    private Employee[] employees;

    public NameListService() {
//        根据项目提供的Data类构建相应大小的employees数组
//        再根据Data类中的数据构建不同的对象，包括Employee、Programmer、Designer和Architect对象，
//        以及相关联的Equipment子类的对象将对象存于数组中
        employees=new Employee[EMPLOYEES.length];

        for (int i = 0; i < employees.length; i++) {
            int type=Integer.parseInt(EMPLOYEES[i][0]);
            int id=Integer.parseInt(EMPLOYEES[i][1]);
            String name=EMPLOYEES[i][2];
            int age=Integer.parseInt(EMPLOYEES[i][2]);
            double salary=Double.parseDouble(EMPLOYEES[i][3]);
            Equipment equipment;
            int stock;
            Double bonus;
            switch (type){
                case EMPLOYEE:
                    employees[i]=new Employee(id,name,age,salary);
                    break;
                case PROGRAMMER:
                    equipment=createequipment(i);
                    employees[i]=new Progammer(id,name,age,salary,equipment);
                    break;
                case DESIGNER:
                    bonus=Double.parseDouble(EMPLOYEES[i][5]);
                    equipment=createequipment(i);
                    employees[i]=new Designer(id,name,age,salary,bonus,equipment);
                    break;
                case ARCHITECT:
                    stock=Integer.parseInt(EMPLOYEES[i][6]);
                    bonus=Double.parseDouble(EMPLOYEES[i][5]);
                    equipment=createequipment(i);
                    employees[i]=new Architect(id,name,age,salary,bonus,stock,equipment);
                    break;
            }

        }
    }

    private Equipment createequipment(int index) {
        int key=Integer.parseInt(EMPLOYEES[index][0]);
        switch (key){
            case PC:
                return new PC(EMPLOYEES[index][1],EMPLOYEES[index][2]);
            case NOTEBOOK:
                return new NoteBook(EMPLOYEES[index][1],Double.parseDouble(EMPLOYEES[index][2]));
            case PRINTER:
                return new Printer(EMPLOYEES[index][1],EMPLOYEES[index][2]);
        }
        return null;
    }

    public Employee[] getAllEmployees(){
        return employees;
    }
    public Employee getEmployee(int id) throws TeamException{
        for (int i = 0; i < employees.length; i++) {
            if(employees[i].getId()==id){
                return employees[i];
            }
        }
        throw new TeamException("找不到指定的员工");
    }

}
